package com.gwz.service.taskService;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gwz.mapper.NewsMapper;
import com.gwz.model.NewsModel;
import com.gwz.util.HtmlUtil;

/**
 * 51cto blog文章最新文章抓取业务实现类
 * @author panguixiang
 *
 */
@Service("ct51TaskServiceImp")
public class Ct51TaskServiceImp implements TaskService{


	private static Logger logger = Logger.getLogger(A5TaskServiceImp.class);
	
	private static String ctoDomain="http://blog.51cto.com/";

	private static String cto51="51cto";
	
	@Autowired
	private NewsMapper newsMapper;
	
	/**
	 * 业务入口
	 */
	public void doTask(String extendParams) {
		cto51(extendParams);
	}

	/**
	 * 解析51cto blog官网首页
	 * @param extendParams
	 */
	private void cto51(String extendParams){
        Document doc,docCh;
        Element aEle;
        Element masthead =null;
        Elements list = null;
        NewsModel model = null;
		try {
			doc = Jsoup.connect(ctoDomain).userAgent("Mozilla").timeout(3000).get();
			masthead = doc.select("div.newslist").first();
			list = masthead.getElementsByTag("li");
		} catch (IOException e) {
			logger.error(e);
			e.printStackTrace();
		}
		Elements objAList=null;
      //遍历标签
        for (Element link : list) {
        	objAList=link.getElementsByTag("a");
        	if(objAList==null || objAList.size()<2) {
        		continue;
        	}
        	aEle = objAList.get(1);
    		model = new NewsModel();
    		model.setType(cto51);
    		try {
    			docCh = Jsoup.connect(aEle.attr("href")).userAgent("Mozilla").timeout(3000).get();
    			model.setTitle(docCh.title());
    			model.setDescription(docCh.select("meta[name=description]").get(0).attr("content"));
    			model.setKey(docCh.select("meta[name=keywords]").get(0).attr("content"));
    			model.setContent(docCh.select("div.showContent").first().html());
    			model.setFrom(1);
    			HtmlUtil.createNewsHtml(extendParams,model,ctoDomain);
    		} catch (IOException e) {
    			logger.error(e);
    		}
    		newsMapper.saveNews(model);
        }
    }

}
